<template>
    <view>
		<shop-cart ref="shopCart" :selectKey="selectKey" :cart-list="cartList" :select-cart-list="selectCartList"
		@operateCount="operateCount" @triggerSelect="triggerSelect" @triggerSelectAll="triggerSelectAll"
		@toDetail="toDetail" @submit="submit" @cartDelete="cartDelete"></shop-cart>
			
    </view>
</template>

<script>
    export default {
        data() {
            return {
				
                selectKey: "_id",
                selectCartList: [],
                cartList: [{
                    _id: '1',
                    count: 1,
                    shopName: '豹纹毛衣',
					color: '咖色',
					size: 'L',
                    shopTagList: [{
                        _id: '1',
                        category: "复古"
                    }],
                    price: 178,
                    shopImage: "../../static/image/shop/cloth.png"
                }, {
                    _id: '2',
                    count: 1,
                    shopName: '小树熊套装',
					color: '黑色',
					size: 'XL',
                    shopTagList: [ {
                        _id: '2',
                        category: "连衣裙"
                    }],
                    price: 39.9,
                    shopImage: "../../static/image/shop/paja.png"
                },
				{
				    _id: '3',
				    count: 1,
				    shopName: '奥特曼马甲',
					color: '灰色',
					size: 'M',
				    shopTagList: [{
				        _id: '3',
				        category: "复古"
				    }],
				    price: 60,
				    shopImage: "../../static/image/shop/ultrama.png"
				},
				{
				    _id: '4',
				    count: 1,
				    shopName: '黑色熊猫套装',
					color: '黑白',
					size: 'M',
				    shopTagList: [{
				        _id: '4',
				        category: "复古"
				    }],
				    price: 57.5,
				    shopImage: "../../static/image/shop/panda.png"
				},
				{
				    _id: '5',
				    count: 1,
				    shopName: '小黄鸭马甲',
					color: '黑白',
					size: 'M',
				    shopTagList: [{
				        _id: '5',
				        category: "复古"
				    }],
				    price: 29.9,
				    shopImage: "../../static/image/shop/yellowduck.png"
				},
				{
				    _id: '6',
				    count: 1,
				    shopName: '轻薄款小黄鸭羽绒服',
					color: '黄色',
					size: 'M',
				    shopTagList: [{
				        _id: '6',
				        category: "复古"
				    }],
				    price: 59.9,
				    shopImage: "../../static/image/shop/yellowcotton.png"
				},],
            };
        },
        methods: {
            submit() {
                const selectList = this.cartList.filter((item) => this.selectCartList.includes(item[this.selectKey]))
                console.log('-submit-', selectList)
            },
            toDetail(item) {
                console.log('-toDetail-', item)
            },
            cartDelete(index) {
                this.cartList.splice(index, 1)
                this.$refs.shopCart.calculatePrice()
            },
            operateCount(params) {
                const {
                    item,
                    type
                } = params
                if (type === 'add') {
                    item.count += 1
                } else if (type === 'del' && item.count >= 2) {
                    item.count -= 1
                }
                this.$refs.shopCart.calculatePrice()
            },
            triggerSelect(item) {
                const findIndex = this.selectCartList.findIndex(t => t === item[this.selectKey])
                if (findIndex === -1) {
                    this.selectCartList.push(item[this.selectKey])
                } else {
                    this.selectCartList.splice(findIndex, 1)
                }
            },
            triggerSelectAll(selectAll) {
                if (selectAll) {
                    this.selectCartList = []
                } else {
                    this.selectCartList = this.cartList.map(item => item[this.selectKey])
                }
            }
        }
    }
</script>

<style lang="scss" scoped>
    page {
        background: rgb(246, 246, 246);
        padding-bottom: 150rpx;
    }
</style>